{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4613abdd",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\FANG\\AppData\\Local\\Temp\\ipykernel_4988\\1125980887.py:29: DeprecationWarning: `np.int0` is a deprecated alias for `np.intp`.  (Deprecated NumPy 1.24)\n",
      "  box = np.int0(box)\n"
     ]
    }
   ],
   "source": [
    "import cv2 as cv\n",
    "import numpy as np\n",
    "\n",
    "filePath = '../data/numberOne.png'\n",
    "img = cv.imread(filePath)\n",
    "imgGray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)\n",
    "\n",
    "thresh, maxValue = 127, 255\n",
    "threshType = cv.THRESH_BINARY_INV\n",
    "ret, thresh = cv.threshold(imgGray, thresh, maxValue, threshType)  #二值化\n",
    "\n",
    "retrievalMode = cv.RETR_TREE\n",
    "approxMethod = cv.CHAIN_APPROX_NONE\n",
    "contours, hierachy = cv.findContours(thresh, retrievalMode, approxMethod)\n",
    "\n",
    "oneIdx = 0\n",
    "cnt = contours[oneIdx]\n",
    "#获取直立边界矩形参数\n",
    "x,y,w,h = cv.boundingRect(cnt)\n",
    "#获取倾斜边界矩形参数\n",
    "center,size,angle = cv.minAreaRect(cnt)\n",
    "\n",
    "#\n",
    "imgBackGround = np.zeros(img.shape, np.uint8)\n",
    "cv.drawContours(imgBackGround, contours, oneIdx, (255,255,255), -1)\n",
    "\n",
    "#draw rotated box\n",
    "box = cv.boxPoints((center,size,angle))     #angle 角度\n",
    "box = np.int0(box)\n",
    "cv.drawContours(imgBackGround,[box],0,(0,0,255),1)\n",
    "\n",
    "#prepare a drawable color image\n",
    "imgNew = np.zeros(img.shape, np.uint8)\n",
    "cv.drawContours(imgNew, contours, oneIdx, (255,255,255), -1)\n",
    "cv.drawContours(imgNew, [box],0, (0,255,0),1)\n",
    "\n",
    "#rectify angle by warpAffine使用倾斜矩形的倾斜角度和中心，通过2d仿射变换对图像进行旋转\n",
    "M = cv.getRotationMatrix2D(center, angle, scale=1)\n",
    "dst = cv.warpAffine(imgNew, M, (img.shape[1],img.shape[0]))\n",
    "\n",
    "cv.imshow(\"thresh\", thresh)\n",
    "cv.imshow(\"img\", img)\n",
    "cv.imshow(\"imgBG\", imgBackGround)\n",
    "cv.imshow(\"dst\", dst)\n",
    "cv.waitKey(0)\n",
    "cv.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3eacfecf",
   "metadata": {},
   "outputs": [],
   "source": [
    "#在黑色图像上，发出所有轮廓\n",
    "import cv2 as cv\n",
    "import numpy as np\n",
    "\n",
    "filePath = '../data/numberOne.png'\n",
    "img = cv.imread(filePath)\n",
    "imgGray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)\n",
    "\n",
    "thresh, maxValue = 127, 255\n",
    "threshType = cv.THRESH_BINARY\n",
    "ret, thresh = cv.threshold(imgGray, thresh, maxValue, threshType)\n",
    "\n",
    "retrievalMode = cv.RETR_TREE\n",
    "approxMethod = cv.CHAIN_APPROX_NONE\n",
    "contours, hierachy = cv.findContours(thresh, retrievalMode, approxMethod)\n",
    "\n",
    "#画出所有轮廓\n",
    "imgBackGround = np.zeros(img.shape, np.uint8)\n",
    "cv.drawContours(imgBackGround, contours, -1, (0,0,255), 2)\n",
    "\n",
    "cv.imshow(\"imgBackGround\", imgBackGround)\n",
    "cv.waitKey(0)\n",
    "cv.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e879293d",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\FANG\\AppData\\Local\\Temp\\ipykernel_4988\\1982057939.py:31: DeprecationWarning: `np.int0` is a deprecated alias for `np.intp`.  (Deprecated NumPy 1.24)\n",
      "  box = np.int0(box)\n"
     ]
    }
   ],
   "source": [
    "import cv2 as cv\n",
    "import numpy as np\n",
    "\n",
    "filePath = '../data/numberOne.png'\n",
    "img = cv.imread(filePath)\n",
    "imgGray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)\n",
    "\n",
    "thresh, maxValue = 127, 255\n",
    "threshType = cv.THRESH_BINARY_INV\n",
    "ret, thresh = cv.threshold(imgGray, thresh, maxValue, threshType)\n",
    "\n",
    "retrievalMode = cv.RETR_TREE\n",
    "approxMethod = cv.CHAIN_APPROX_NONE\n",
    "contours, hierachy = cv.findContours(thresh, retrievalMode, approxMethod)\n",
    "\n",
    "cnt = contours[1]\n",
    "#获取直立边界矩形参数\n",
    "x,y,w,h = cv.boundingRect(cnt)\n",
    "#获取倾斜边界矩形参数\n",
    "center,size,angle = cv.minAreaRect(cnt)\n",
    "\n",
    "\n",
    "imgBackGround = np.zeros(img.shape, np.uint8)\n",
    "cv.drawContours(imgBackGround, contours, 1, (255,255,255), -1)\n",
    "\n",
    "#画出直立矩形\n",
    "cv.rectangle(imgBackGround,(x,y),(x+w,y+h),(0,255,0),2)\n",
    "\n",
    "#画出倾斜矩形\n",
    "box = cv.boxPoints((center,size,angle))\n",
    "box = np.int0(box)\n",
    "cv.drawContours(imgBackGround,[box],0,(0,0,255),2)\n",
    "\n",
    "cv.imshow(\"img\", img)\n",
    "cv.imshow(\"imgBG\", imgBackGround)\n",
    "cv.waitKey(0)\n",
    "cv.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "535f9a9f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
